package com.hsw.habit.service.impl;

import com.hsw.habit.mapper.TaskGroupsMapper;
import com.hsw.habit.model.TaskGroups;
import com.hsw.habit.model.TaskGroupsExample;
import com.hsw.habit.model.bean.ListResponse;
import com.hsw.habit.service.GroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.logging.Logger;

@Service
public class GroupServiceImpl implements GroupService {
    @Resource
    private TaskGroupsMapper groupMapper;

    Logger logger = Logger.getLogger("groupService");

    @Override
    public TaskGroups selectGroupById(Integer id) {
        return groupMapper.selectByPrimaryKey(id);
    }

    @Override
    public ListResponse<TaskGroups> selectGroupsByUserId(Integer id) {
        TaskGroupsExample taskGroupsExample = new TaskGroupsExample();
        TaskGroupsExample.Criteria criteria = taskGroupsExample.createCriteria();
        criteria.andUseridEqualTo(id);
        criteria.andIsdeletedEqualTo((short) 0);
        taskGroupsExample.setOrderByClause("created desc");
        ListResponse<TaskGroups> listResponse = new ListResponse<>();
        listResponse.setList(groupMapper.selectByExample(taskGroupsExample));
        listResponse.setEnd(true);
        return listResponse;
    }

    @Override
    public List<TaskGroups> selectByUserId(Integer userId) {
        TaskGroupsExample taskGroupsExample = new TaskGroupsExample();
        TaskGroupsExample.Criteria criteria = taskGroupsExample.createCriteria();
        criteria.andUseridEqualTo(userId);
        return groupMapper.selectByExample(taskGroupsExample);
    }

    @Override
    public boolean updateGroup(TaskGroups groups) {
        int res = groupMapper.updateByPrimaryKey(groups);
        return res == 1;
    }

    @Override
    public boolean deleteGroup(Integer id) {
        int res = groupMapper.softDeleteByPrimaryKey(id);
        return res == 1;
    }

    @Override
    public boolean insertGroup(TaskGroups groupModel) {
        int res = groupMapper.insert(groupModel);
        return res == 1;
    }
}
